package com.arcpan.Service.Impl;

import com.arcpan.Constant.BaseConstant;
import com.arcpan.Entity.Dto.SessionWebUserDto;
import com.arcpan.Entity.Dto.SysSettingDto;
import com.arcpan.Entity.Dto.UploadResultDto;
import com.arcpan.Entity.Dto.UserSpaceDto;
import com.arcpan.Entity.UserInfo;
import com.arcpan.Exception.BaseException;
import com.arcpan.Mapper.FileInfoMapper;
import com.arcpan.Mapper.UserInfoMapper;
import com.arcpan.Service.EmailCodeService;
import com.arcpan.Service.UserInfoService;
import com.arcpan.Util.StringTools;
import com.arcpan.component.RedisComponent;

import com.arcpan.config.AppConfig;
import com.arcpan.enums.UserStatusEnum;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private EmailCodeService emailCodeService;
    @Resource
    private RedisComponent redisComponent;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private AppConfig appConfig;
    @Resource
    private FileInfoMapper fileInfoMapper;


    @Transactional
    public void register(String email, String nickName, String password, Integer emailCode) {
        UserInfo userInfo = userInfoMapper.selectByEmail(email);
        if (null != userInfo) {
            throw new BaseException("账号已存在");
        }
        UserInfo userInfo1 = userInfoMapper.selectByNickName(nickName);
        if (null != userInfo1) {
            throw new BaseException("昵称已存在");
        }
        //校验邮箱验证码
        emailCodeService.checkCode(email, emailCode);
        String userId = StringTools.getRandomNumber(BaseConstant.LENGTH_10);
        UserInfo userInfo2 = new UserInfo();
        userInfo2.setUserId(userId);
        userInfo2.setNickName(nickName);
        userInfo2.setEmail(email);
        userInfo2.setPassword(StringTools.encodeByMd5(password));
        userInfo2.setJoinTime(new Date());
        userInfo2.setStatus(UserStatusEnum.ENABLE.getStatus());
        userInfo2.setUseSpace(0L);
        SysSettingDto sysSettingDto = redisComponent.getsysSettingDto();
        userInfo2.setTotalSpace(sysSettingDto.getUserInitUserSpace() * BaseConstant.MB);
        userInfoMapper.insert(userInfo2);


    }

    public SessionWebUserDto login(String email, String password) {
        UserInfo userInfo = userInfoMapper.selectByEmail(email);
        if (null == userInfo || !password.equals(userInfo.getPassword())) {
            throw new BaseException("用户不存在");
        }
        if (UserStatusEnum.DISABLE.getStatus().equals(userInfo.getStatus())) {
            throw new BaseException("用户被禁用");

        }
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setLastLoginTime(new Date());
        userInfoMapper.updateByUserId(userInfo1, userInfo.getUserId());
        SessionWebUserDto sessionWebUserDto = new SessionWebUserDto();
        sessionWebUserDto.setNickName(userInfo.getNickName());
        sessionWebUserDto.setUserId(userInfo.getUserId());
        if (ArrayUtils.contains(appConfig.getAdminemail().split(","), email)) {
            sessionWebUserDto.setAdmin(true);
        } else {
            sessionWebUserDto.setAdmin(false);

        }
        UserSpaceDto userSpaceDto = new UserSpaceDto();
//        userSpaceDto.setUseSpace();
        //todo
        Long userSpace=fileInfoMapper.selectUseSpace(userInfo.getUserId());
        userSpaceDto.setUseSpace(userSpace);
        userSpaceDto.setTotalSpace(userInfo.getTotalSpace());
        redisComponent.saveUserSpaceuse(userInfo.getUserId(), userSpaceDto);
        return sessionWebUserDto;


    }

    @Transactional
    public void resetPwd(String email, String password, Integer emailCode) {
        UserInfo userInfo = userInfoMapper.selectByEmail(email);
        if (null == userInfo) {
            throw new BaseException("用户不存在");
        }
        emailCodeService.checkCode(email, emailCode);
        UserInfo updateInfo = new UserInfo();
        updateInfo.setPassword(DigestUtils.md5Hex(password));
        userInfoMapper.updateByUserId(updateInfo, userInfo.getUserId());
    }

    public UserSpaceDto getspace(String userId) {
        UserSpaceDto userSpaceDto=redisComponent.getUserSpace(userId);
        return userSpaceDto;
    }

    @Override
    public SessionWebUserDto qqlogin(String code) {
        return null;
    }

}
